package com.meng.viewpager_define_demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<View> views;
    private DefineViewPager viewPager;
    private MyPagerAdapter mAdapter;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (DefineViewPager) findViewById(R.id.viewPager);
        mAdapter = new MyPagerAdapter(getViews());
        viewPager.setAdapter(mAdapter);
        et = (EditText) findViewById(R.id.et);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setmCurShowChildIndex(Integer.parseInt(et.getText().toString()));
            }
        });
    }

    private List<View> getViews() {
        List<View> views = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(this);
        int imgIds[] = {R.drawable.page1,R.drawable.page2,R.drawable.page3,R.drawable.page4,R.drawable.page5};
        for(int id:imgIds){
            View view = inflater.inflate(R.layout.pager1, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.img);
            imageView.setImageResource(id);
            views.add(view);
        }
        return views;
    }
}
